import storage from './storage';

export const qs = (obj) => {
  let qsList = [];
  Object.keys(obj || {}).forEach(key => {
    qsList.push(`${key}=${obj[key]}`)
  })
  return qsList.join('&')
}

export const request = async (params) => {
  let url = params.url;
  let method = params.method || 'GET';
  try {
    const config = {
      method,
      body: params.data,
      headers: {
        'user-agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1',
        'Content-Type': 'application/x-www-form-urlencoded',
        ...(params.headers || {})
      }
    }
    console.log(url, config)
    const ret = await fetch(url, config);
    const res = await ret.text()
    console.log('http success ----------------------')
    console.log(url, res)
    return res
  } catch (error) {
    console.log('http error -------------------')
    console.log(url, error)
    console.log(config)
    throw error
  }
}
